using System;
//using System.Data.Entity.Validation;

namespace AllIsWell.Persistance.Ef
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly MyDiary _myDiary;
        private readonly Lazy<IDestinyRepository> _destinyRepository;
        private readonly Lazy<IWhatRepository> _whatRepository;
        private readonly Lazy<IPageRepository> _pageRepository;

        public UnitOfWork()
        {
            _myDiary = new MyDiary();
            _destinyRepository = new Lazy<IDestinyRepository>(() => new DestinyRepository(_myDiary));
            _whatRepository = new Lazy<IWhatRepository>(() => new WhatRepository(_myDiary));
            _pageRepository = new Lazy<IPageRepository>(() => new PageRepository(_myDiary));
        }

        public IDestinyRepository DestinyRepository
        {
            get { return _destinyRepository.Value; }
        }

        public IWhatRepository WhatRepository
        {
            get { return _whatRepository.Value; }
        }

        public IPageRepository PageRepository
        {
            get { return _pageRepository.Value; }
        }

        public void Save()
        {
            //try
            //{
                _myDiary.SaveChanges();
            //}
            //catch (DbEntityValidationException e)
            //{
                //throw;
            //}
        }
    }
}